﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SQLite;

namespace LuZhouLaoJiao
{
    public partial class ManageUser : Form
    {
        public ManageUser()
        {
            InitializeComponent();
        }
        public DataTable dt = null;
        //MySqlConnection conn = null;
        //MySqlCommand comm = null;
        //MySqlDataReader reader = null;

        SQLiteConnection conn = null;
        SQLiteCommand comm = null;
        SQLiteDataReader reader = null;

//         string strCon = null;
//         string dbUser = Global.getRegeditValue("localDBUser");
//         string dbPwd = Global.getRegeditValue("localDBPwd");
//         string database = Global.getRegeditValue("localDatabase");
//         string ip = Global.getRegeditValue("localIP");

        private void ManageUser_Load(object sender, EventArgs e)
        {
            lbLoginer.Text = "User: " + Global.userName;
            loadUser();
        }

        private void loadUser()
        {
            //strCon = "server = " + ip + "; user id = " + dbUser + "; password = " + dbPwd + "; database = " + database + "; persist security info = false";
            //conn = new MySqlConnection(strCon);
            //comm = new MySqlCommand();

            conn = new SQLiteConnection("Data Source=./data.db");
            comm = new SQLiteCommand();
            //MessageBox.Show("sqlite");
            conn.Open();
            comm = conn.CreateCommand();
            comm.Connection = conn;
            comm.CommandText = "select * from user;";
            try
            {
                reader = comm.ExecuteReader();
                dt = new DataTable();
                dt.Load(reader);
                dt.Columns[0].ColumnName = "编号";
                dt.Columns[1].ColumnName = "用户名";             
                dt.Columns[3].ColumnName = "是否管理员";

                dataGridView1.DataSource = dt;
                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].Visible = false;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("数据库读取错误(BI)：" + ex.Message);
            }
            finally
            {
                //reader.Close();
                //conn.Close();
            }
        }

        private void 添加管理员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateAdminUser admin = new CreateAdminUser();
            admin.ShowDialog();
            loadUser();
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateNormalUser user = new CreateNormalUser();
            user.ShowDialog();
            loadUser();
        }

        private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString().Trim() == Global.userName)
            {
                MessageBox.Show("用户不能删除自己.");
                return;
            }
            else
            {
                DialogResult result = MessageBox.Show("确定要删除用户" + dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString() + "?", "删除用户", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    comm.CommandText = "delete from user where id = " + dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString().Trim() + ";";
                    try
                    {
                        int i = comm.ExecuteNonQuery();
                        if (i > 0)
                        {
                            MessageBox.Show("用户" + dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString() + "已经被删除.");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("删除用户出现错误(BI)" + ex.Message);
                    }
                    loadUser();
                }
            }
        }

        private void ManageUser_FormClosed(Object sender, FormClosedEventArgs e)
        {
            reader.Close();
            conn.Close();
            //MessageBox.Show("窗口关闭.");
        }
    }
}
